﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cludg.Web.Core.ViewModels.Extensions
{
	public class ViewModelExtensionRegistry : IViewModelExtensionRegistry
	{
		protected readonly List<ViewModelExtension> Extensions = new List<ViewModelExtension>();

		protected readonly IEnumerable<IViewModelExtensionProvider> Providers;

		protected void RegisterProvider(IViewModelExtensionProvider provider)
		{
			foreach (var ext in provider.GetExtensions())
				Register(ext);
		}

		public ViewModelExtensionRegistry(IEnumerable<IViewModelExtensionProvider> providers)
		{
			Providers = providers;
			foreach (var provider in Providers)
				RegisterProvider(provider);
		}

		public void Register(ViewModelExtension extension)
		{
			Extensions.Add(extension);
		}

		public void Initialize(BaseModel model)
		{
			foreach (var ext in Extensions.Where(x => x.IsFor(model)))
				ext.Initialize(model);
		}

	}
}
